package com.ruoyi.mymod.controller.phonelogin;


import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.mymod.service.IPhoneLoginService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;


/**
 * 测试功能
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/phoneLogin")
public class PhoneLoginController extends BaseController
{
    @Autowired
    private IPhoneLoginService phoneloginService;

    @PostMapping("/login")
    public AjaxResult phoneLogin(@RequestBody LoginBody loginBody) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        if (loginBody.getCode()!=null && "NT".equals(loginBody.getCode())){
            if (loginBody.getUsername()!=null && loginBody.getUsername().length()>1){
                // 生成令牌
                String token = phoneloginService.ntphonelogin(loginBody.getUsername(),loginBody.getPassword());
                ajax.put(Constants.TOKEN, token);
            }else {
                try {
                    Object responseBoday = phoneloginService.getSessionKey(loginBody.getPassword(),loginBody.getCode());
                    Map responseBoday1 = (Map) responseBoday;
                    Object phone_info = responseBoday1.get("phone_info");
                    Map phone_info1 = (Map) phone_info;
                    String phoneNumber = phone_info1.get("phoneNumber").toString();
                    String token = phoneloginService.ntphonelogin(phoneNumber,loginBody.getPassword());
                    ajax.put(Constants.TOKEN, token);
                }catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }else if (loginBody.getCode()!=null && "YW".equals(loginBody.getCode())){
            if (loginBody.getUsername()!=null && loginBody.getUsername().length()>1){
                // 生成令牌
                String token = phoneloginService.phonelogin(loginBody.getUsername());
                ajax.put(Constants.TOKEN, token);
            }else {
                try {
                    Object responseBoday = phoneloginService.getSessionKey(loginBody.getPassword(),loginBody.getCode());
                    Map responseBoday1 = (Map) responseBoday;
                    Object phone_info = responseBoday1.get("phone_info");
                    Map phone_info1 = (Map) phone_info;
                    String phoneNumber = phone_info1.get("phoneNumber").toString();
                    String token = phoneloginService.phonelogin(phoneNumber);
                    ajax.put(Constants.TOKEN, token);
                }catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }else if (loginBody.getCode()!=null && "CHE".equals(loginBody.getCode())){
            if (loginBody.getUsername()!=null && loginBody.getUsername().length()>1){
                // 生成令牌
                String token = phoneloginService.chephonelogin(loginBody.getUsername(),loginBody.getPassword());
                ajax.put(Constants.TOKEN, token);
            }else {
                try {
                    Object responseBoday = phoneloginService.getSessionKey(loginBody.getPassword(),loginBody.getCode());
                    Map responseBoday1 = (Map) responseBoday;
                    Object phone_info = responseBoday1.get("phone_info");
                    Map phone_info1 = (Map) phone_info;
                    String phoneNumber = phone_info1.get("phoneNumber").toString();
                    String token = phoneloginService.chephonelogin(phoneNumber,loginBody.getPassword());
                    ajax.put(Constants.TOKEN, token);
                }catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        return ajax;
    }





}
